package com.cakeclient;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.actionbarsherlock.app.SherlockFragment;
import com.cakeclient.Model.Client;

import java.util.List;

public class ClientManageAccountsFragment extends SherlockFragment
{
    public List<Client> getAccountList() {
        return Client.getAll(getSherlockActivity().getApplicationContext());
    }
    ListView listView;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.client_manage_account_layout,container,false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.

        listView = (ListView) getSherlockActivity().findViewById(R.id.client_manage_accounts_listview);


        displayAccounts(getAccountList());

        Button buttonAdd = (Button) getSherlockActivity().findViewById(R.id.client_manage_account_addaccount_button);
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((MainParametersActivity) getActivity()).LaunchRegisterFragment(view);
            }
        });


    }


    public void displayAccounts(List<Client> client){
        ListAdapter adapter = new ListViewClientAccountsAdapter(getSherlockActivity(), R.layout.client_listview_item_account, client);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Client.setLocalClient(getSherlockActivity().getApplicationContext(), getAccountList().get(i));
                displayAccounts(getAccountList());
                Toast.makeText(getSherlockActivity(), "User " +  getAccountList().get(i).getName() + " selected.", Toast.LENGTH_LONG).show();
            }
        });
    }
}

